package com.peanut.baby.mvp.calendar;

import android.app.Activity;

import com.peanut.baby.InitManager;
import com.peanut.baby.db.DBManager;
import com.peanut.baby.http.BaseObserver;
import com.peanut.baby.http.BaseScheduler;
import com.peanut.baby.http.RetrofitClient;
import com.peanut.baby.model.CalendarRecord;

import java.util.List;

import cn.nekocode.rxlifecycle.RxLifecycle;
import io.reactivex.Observable;

/**
 * Created by anonymouslystudio on 2017/9/6.
 */

public class CalendarPresenter implements CalendarContract.Presenter{
    CalendarContract.View view;
    private Activity activity;
    public CalendarPresenter(CalendarContract.View view,Activity activity) {
        this.view = view;
        this.activity = activity;
    }

    @Override
    public void updateData(final String date, final String dayima, final String tongfang, final String tiwen, final String yesuan, final String baidai) {
        String dayimaValue = dayima;
        if(dayimaValue.equals("来了")){
            dayimaValue = "是";
        }else if(dayimaValue.equals("走了")){
            dayimaValue = "否";
        }

        Observable obs = RetrofitClient.getInstance().submitDailyRecord(InitManager.getInstance().getUserId(),
                date,
                dayimaValue,
                tongfang,
                tiwen,
                yesuan,
                baidai,
                InitManager.getInstance().getUser().userToken);
        obs.compose(BaseScheduler.<CalendarRecord>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<CalendarRecord>() {
            @Override
            protected void onHandleSuccess(CalendarRecord o) {
                CalendarRecord record = new CalendarRecord();
                record.userId = Integer.parseInt(InitManager.getInstance().getUserId());
                record.date = date;
                record.dayima = dayima;
                record.tiwen = tiwen;
                record.tongfang = tongfang;
                record.yesuan = yesuan;
                record.baidai = baidai;
                DBManager.getInstance().updateCalendarRecord(record);

                view.onSubmitDataSuccess(record);
            }

            @Override
            protected void onHandleError(String code, String msg) {
                view.onSubmitDataFailed(msg);
            }
        });

    }

    @Override
    public void getData() {
        Observable obs = RetrofitClient.getInstance().getDaliyRecords(InitManager.getInstance().getUserId(),
                InitManager.getInstance().getUser().userToken);
        obs.compose(BaseScheduler.<List<CalendarRecord>>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<List<CalendarRecord>>() {
            @Override
            protected void onHandleSuccess(List<CalendarRecord> o) {
                //insert into database
                updateDatabaseInSubThread(o);
            }

            @Override
            protected void onHandleError(String code, String msg) {
                view.onDataGetFailed(msg);
            }
        });
    }

    @Override
    public CalendarRecord getRecordFromDB(int uid, String date) {
        return DBManager.getInstance().getCalendarRecord(uid,date);
    }

    private void updateDatabaseInSubThread(final List<CalendarRecord> o){
        new Thread(new Runnable() {
            @Override
            public void run() {
                if(o != null && !o.isEmpty()){
                    for(int i=0;i<o.size();i++){
                        DBManager.getInstance().updateCalendarRecord(o.get(i));
                    }
                }
                view.onDataGet(o);

            }
        }).start();
    }
}
